package com.matt.repository.estimate;

import com.matt.domain.estimate.CourseExtendEstimate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CourseExtendEstimateRepository extends JpaRepository<CourseExtendEstimate, String> {
    /**
     * 分页 时间倒序
     * @param courseId
     * @return
     */
    @Query(value = "SELECT * FROM course_extend_estimate WHERE courseId = ? ORDER BY createDate DESC", nativeQuery = true)
    List<CourseExtendEstimate> listByCourseId(String courseId);

    // 新增
    @Query(value = "INSERT INTO course_extend_estimate(courseId, evaluatorId, courseContent, createDate) VALUES(?, ?, ?, ?)", nativeQuery = true)
    String addCourseEstimate(String courseId, String evaluatorId, String courseContent, String createDate);

    // 修改
    @Query(value = "UPDATE course_extend_estimate SET(courseContent = ? " +
            "WHERE id = id", nativeQuery = true)
    String updateCourseEstimate(String id, String courseContent);

    // 删除
    @Query(value = "DELETE FROM course_extend_estimate WHERE id = ?1 ", nativeQuery = true)
    String deleteByCourseEstimateId(String estimateId);

    // 详情
    @Query(value = "SELECT * FROM course_extend_estimate WHERE id = ?1", nativeQuery = true)
    CourseExtendEstimate getByCourseEstimateId(String estimateId);

    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE course_extend_estimate SET likeCount = likeCount + 1 WHERE id = ?1 ", nativeQuery = true)
    void increaseLikeCountById(String id);

    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE course_extend_estimate SET likeCount = likeCount - 1 WHERE id = ?1 ", nativeQuery = true)
    void decreaseLikeCountById(String id);
}
